// pages/commentDetail/commentDetail.js
const http = require('../../utils/http.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemList:['举报', '回复'],
    autoFocus:false,
    commentList:[],
    conmentText:'',
    sCommem:false, //是否二级回复
    placeholder:"来条评论，写下你的感想"
  },
  //举报回复
  report(e){
    console.log(e)
    var itemList = this.data.itemList;
    var item=e.currentTarget.dataset.item;
    this.setData({
      item
    })
    wx.showActionSheet({
      itemList,
      itemColor:'#1478F6',
      success:(res)=> {
        if (res.tapIndex==0){
          wx.navigateTo({
            url: "/pages/report/report?reportType=2&typeId="+item.id+'&h_uid='+item.uid
          })
        }else{
          //回复
          this.setData({
            autoFocus:true,
            sCommem:true,
            placeholder: `回复${item.nickname}:`
          })
          
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  //评论详情
  getDetails(){
    http.fetchApi('procedure_t', { p: `R2019009|${this.data.commentItem.id}` }).then(res=>{
      if(res.data.code==1){
        var commentList = res.data.results;
      
        this.setData({
          commentList
        })
      }
    })
  },

  //回复 评论
  commonComment(conment_user_id, response_comment_id) {
    var post_id = this.data.commentItem.post_id;
    var user_id = app.globalData.uid;
    var conment_content = this.data.conmentText
    http.fetchApi('p_huifu_tz', { p: `R2017008|${post_id}|${user_id}|2|${conment_user_id}|${response_comment_id}|${conment_content}` }).then(res => {
      console.log(`R2017008|${post_id}|${user_id}|2|${conment_user_id}|${response_comment_id}|${conment_content}`)
      if (res.data.code == 1) {
        if (res.data.results[0].codes == 1) {
          wx.showToast({ title: '评论成功' });
          this.setData({
            conmentText: ''
          })
          this.getDetails();

        }else{
          wx.showToast({ title: '网络繁忙', icon: 'none' });
        }
      } else if (res.data.code == 0){
        wx.showToast({ title: res.data.results,icon:'none' });
      }
    })
  },

  handleComment(){
    if (this.data.sCommem){
      this.commonComment(this.data.item.uid, this.data.commentItem.id);
    }else{
      this.commonComment(this.data.commentItem.uid, this.data.commentItem.id)
    }
    
  },
  
  conmentContent(e) {
    var conmentText = e.detail.value;
    this.setData({
      conmentText
    })
  },

  replyLeve1(){
   this.setData({
     sCommem: false,
     placeholder: `来条评论，写下你的感想`
   })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //var rid=options.rid;
    var commentItem = JSON.parse(options.item)
    console.log(JSON.parse(options.item))
    this.setData({
      commentItem
    })
    this.getDetails();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})